#!/bin/bash
source ./constants_ohos
set -ex

# 配置git，防止下载代码失败
sudo apt-get install gnutls-bin
git config --global user.name "leixin"
git config --global user.email "leixin19@huawei.com"
git config --global http.sslVerify false
git config --global http.postBuffer 9048576000


# 测试套路径
WORK_DIR=${PWD}

mkdir -p ${WORK_DIR}/llvm

# 下载llvm代码
cd ${WORK_DIR}/llvm
repo init -u https://gitee.com/OpenHarmony/manifest.git -b master -m llvm-toolchain.xml
repo sync -c 
repo forall -c 'git lfs pull'

# 准备工具
./toolchain/llvm-project/llvm-build/env_prepare.sh

# 编译llvm工具链
python3 ./toolchain/llvm-project/llvm-build/build.py

# 解压linux环境上的工具链
tar -xf ${WORK_DIR}/../../llvm/out/clang-dev-linux-x86_64.tar.bz2
tar -xf ${WORK_DIR}/../../llvm/out/ohos-sysroot-dev.tar.bz2

# llvm-ci仓下载
 git clone git@gitee.com:xwx1135370/llvm-ci.git -b master

sudo chown -R $USER ${LLVM_PROJECT_DIR} *

# OHOS平台
function kill-lldb-server() {
    hdc shell killall -9 lldb-server || true
    hdc shell ps -A | grep lldb_server && ( echo "lldb server wasn't killed";  exit 1 ) || true
}

# TODO_FIX_HACK: need somehow modify "Makefile.rules" to not to include "Android.rules" file
sed -i 's/\(include $(THIS_FILE_DIR)\/Android.rules\)/#\1/g' \
   ${LLDB_PROJECT_DIR}/packages/Python/lldbsuite/test/make/Makefile.rules

# OHOS平台
export OHOS_DEVICE_SERIAL=${OHOS_SERIAL}
if [[ ! "${OHOS_DEVICE_SERIAL}" ]]; then
    # lldb will fail if phone serial will be with colon character in platform_url parameter.
    # It will fail because it expect tcp port number of lldb-server(for example 1234) on remote side,
    # and url like "main-provider.devops-spb.rnd.huawei.com:15121:1234" does not parse correctly.
    # The only way to workaround this is to set serial as environment variable and set only lldb-server
    # port without address in platform_url argument
    false
fi

# OHOS平台
kill-lldb-server
hdc fport tcp:${LLDB_SERVER_PORT_OHOS} tcp:${LLDB_SERVER_PORT_OHOS}
hdc shell "rm -rf ${REMOTE_PLATFORM_WORKDIR} && mkdir ${REMOTE_PLATFORM_WORKDIR}"
hdc file send ${LLDB_SERVER_BINARY_RELATIVE_TO_TOOLCHAIN_PATH} ${REMOTE_PLATFORM_WORKDIR}
hdc shell chmod 777 ${REMOTE_PLATFORM_WORKDIR}/lldb-server

SERVER_LOGFILE=lldb_server.log

jobs


CONFIG_OUT="${LLDB_PROJECT_DIR}"
echo ${CONFIG_OUT}
python3 "${LLVM_CI_SCRIPTS}"/generate_config.py \
        --target "${TARGET}" \
        --default "${LLVM_CI_CONFIGS}/${CONFIG}" \
        --clang "${TOOLCHAIN_PATH}" \
        --sysroot "${SYSROOT}" \
        --out "${LLDB_PROJECT_DIR}"

CONFIG_FILE="${CONFIG_OUT}/${TARGET}.cfg"
CONFIG_OPT="--config ${CONFIG_FILE}"

echo ${CONFIG_FILE}
cat ${CONFIG_FILE}
LOGFILE="${LLDB_PROJECT_DIR}/test.log"

pushd "${LLVM_PROJECT_DIR}"

# clear previous cmake files
rm -rf CMake* *.cmake

cmake -G Ninja \
    -DLLVM_ENABLE_PROJECTS='clang;clang-tools-extra;libcxx;libcxxabi;libunwind;lldb;compiler-rt;lld;polly' \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX="${PWD}/install" \
    "${LLVM_PROJECT_DIR}/llvm"

# just generated
ninja llvm-tblgen
LLVM_DIR="${PWD}/lib/cmake/llvm"

# clear previous cmake files
rm -rf CMake* *.cmake

popd

# clear previous cmake files
rm -rf CMake* *.cmake

LLDB_XML_TEST_OUTPUT="test-results.xml"
disabled_tests_dir="${LLVM_CI_RESOURCES}/disabled_tests"

    LLDB_TEST_USER_ARGS="-v;"
    LLDB_TEST_USER_ARGS+="--arch;${LLDB_API_DOTEST_ARCH};"
    LLDB_TEST_USER_ARGS+="-E;${CONFIG_OPT};"
    LLDB_TEST_USER_ARGS+="--env;ARFLAGS=rvU;"
    LLDB_TEST_USER_ARGS+="--excluded;${disabled_tests_dir}/${DISABLED_TESTS};"
    LLDB_TEST_USER_ARGS+="--platform-name;${LLDB_API_REMOTE_PLATFORM_NAME_OHOS};"
    LLDB_TEST_USER_ARGS+="--platform-url;${LLDB_API_REMOTE_PLATFORM_URL_STRING_OHOS};"
    LLDB_TEST_USER_ARGS+="--platform-working-dir;${REMOTE_PLATFORM_WORKDIR};"

   
    cmake \
    -DCMAKE_C_COMPILER="${TOOLCHAIN_BIN_PATH}/clang" \
    -DCMAKE_CXX_COMPILER="${TOOLCHAIN_BIN_PATH}/clang++" \
    -DLLVM_DIR="${LLVM_DIR}" \
    -DLLDB_DEFAULT_TEST_ARCH="${LLDB_API_DOTEST_ARCH}" \
    -DCMAKE_AR="${TOOLCHAIN_BIN_PATH}/llvm-ar" \
    -DCMAKE_OBJCOPY="${TOOLCHAIN_BIN_PATH}/llvm-objcopy" \
    -DLLVM_TABLEGEN_EXE="${LLVM_PROJECT_DIR}/bin/llvm-tblgen" \
    -DLLDB_TEST_EXECUTABLE="${TOOLCHAIN_BIN_PATH}/lldb" \
    -DLLDB_TEST_COMPILER="${TOOLCHAIN_BIN_PATH}/clang" \
    -DLLDB_TEST_DSYMUTIL="${TOOLCHAIN_BIN_PATH}/dsymutil" \
    -DLLDB_TEST_FILECHECK="${TOOLCHAIN_BIN_PATH}/FileCheck" \
    -DLLDB_TEST_YAML2OBJ="${TOOLCHAIN_BIN_PATH}/yaml2obj" \
    -DLLDB_TEST_SERVER="${REMOTE_PLATFORM_WORKDIR}/lldb-server" \
    -DLLDB_TEST_USER_ARGS="${LLDB_TEST_USER_ARGS}" \
    "${LLDB_PROJECT_DIR}"

	
    hdc shell \
    ${REMOTE_PLATFORM_WORKDIR}/lldb-server \
    platform \
    --listen "*:${LLDB_SERVER_PORT_OHOS}" \
    --server  > ${SERVER_LOGFILE} 2>&1 &
    hdc_lldb_server_pid=$!
    
    #如果存在dotest可执行文件，则run测试用例
    if [ -f "${LLDB_TEST_ROOT}/bin/lldb-dotest" ] ; then
    	${LLDB_TEST_ROOT}/bin/lldb-dotest ${LLDB_PROJECT_DIR}/test/API/functionalities/data-formatter/data-formatter-stl
    	${LLDB_TEST_ROOT}/bin/lldb-dotest ${LLDB_PROJECT_DIR}/test/API/ohos

    fi
